<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Price_Techan extends ORM
{
    /**
     * @param $params
     * @return array
     * 团购产品列表
     */
    public static function channel_product_list($params)
    {
        $params = json_decode($params,true);
        $date = strtotime(date('Ymd'));
        $page = ($params['page']-1)*$params['page_size'];

        $sql = "SELECT a.id,a.title,a.status,a.ishidden FROM sline_techan AS a ";
        $sql.= "LEFT JOIN sline_techan_suit AS b ON(a.id=b.productid) ";
        $sql.= "WHERE a.supplierlist={$params['supplier_id']} AND b.price>0 AND (b.number>0 OR b.number='-1') ";
        if($params['keyword']){
            $sql.= "AND (a.title LIKE '%{$params['keyword']}%' OR a.id='{$params['keyword']}') ";
        }
        $sql.= "GROUP BY a.id ";
        $sql.= "ORDER BY a.modtime DESC,a.addtime DESC ";
        $total = DB::query(1, $sql)->execute()->count();
        $sql.= "LIMIT {$page},{$params['page_size']} ";
        $list = DB::query(1, $sql)->execute()->as_array();
        if(!$list){
            return array('status'=>false,'msg'=>'没有匹配的特产商品！');
        }
        foreach ($list as &$v){
            $v['status_name'] = Model_Supplier::status_name($v['status']);
            $v['time_validity'] = '长期有效';
        }

        return array(
            'status'     => true,
            'list'       => $list,
            'total_page' => ceil($total/$params['page_size'])
        );
    }
}